<html>
    <?php
if(!isset($_SESSION)) session_start();
    require('../../Smarty/libs/Smarty.class.php');

    $smarty = new Smarty;

    $smarty->setTemplateDir('./smarty/templates/');
    $smarty->setCompileDir('./smarty/templates_c/');
    $smarty->setConfigDir('./smarty/configs/');
    $smarty->setCacheDir('./smarty/cache/');
    $smarty->display('preHeader.tpl');

    if (empty($_SESSION['logged_bool'])) {
        $_SESSION['logged_bool'] = false;
    }
    $status = $_SESSION['logged_bool'];

    $smarty->assign("logged", $status);



    $email = "";
    $itineraryId = "";
    $continue = true;
    //$email = "andrei.puf@xoomworks.com";
    //$itineraryId = "104345359";
    if (!empty($_GET['email'])) {
        $email = $_GET['email'];
    } else {
        $continue = false;
    }
    if (!empty($_GET['itin'])) {
        $itineraryId = $_GET['itin'];
    } else {
        $continue = false;
    }

    if ($continue) {
        try {
            // Get itinerary
            include_once 'API/APIObject.php';
            $api = new APIObject();

            $itinRes = $api->GetItinerary($itineraryId, $email);
            //$api->PrintTestDataOnScreen($itinRes, "");
            // $itinRes->Itinerary->HotelConfirmation->numberOfAdults . " Adult(s)",
            //$api->PrintTestDataOnScreen($itinRes->Itinerary->HotelConfirmation[0]->Hotel[0], "");
            $rooms = array();
            $confirmationNumber = array();
            $totalPrice = 0;
            $taxes = 0;
            $numberOfRooms = count($itinRes->Itinerary->HotelConfirmation);
            if ($numberOfRooms > 1) {
                for ($i = 0; $i < $numberOfRooms; $i++) {
                    $rooms[$i]['numberOfAdults'] = $itinRes->Itinerary->HotelConfirmation[$i]->numberOfAdults;
                    $rooms[$i]['numberOfChildren'] = $itinRes->Itinerary->HotelConfirmation[$i]->numberOfChildren;
                    $rooms[$i]['firstName'] = $itinRes->Itinerary->HotelConfirmation[$i]->ReservationGuest->firstName;
                    $rooms[$i]['lastName'] = $itinRes->Itinerary->HotelConfirmation[$i]->ReservationGuest->lastName;
                    $confirmationNumber[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->confirmationNumber;
                    $roomDescription[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->roomDescription;
                    $tripAdvisor[$i] = "<b>Trip Advisor Info:This hotel is rated: " . $itinRes->Itinerary->HotelConfirmation[$i]->Hotel[$i]->tripAdvisorRating . "</b>";
                    $checkInDate[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->arrivalDate;
                    $checkOutDate[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->departureDate;
                    $numberOfNights[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->nights;
                    $currencyCode[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->RateInfos->RateInfo->ChargeableRateInfo->{'@currencyCode'};
                    $totalPrice += (float) $itinRes->Itinerary->HotelConfirmation[$i]->RateInfos->RateInfo->ChargeableRateInfo->{'@total'};
                    if (isset($itinRes->Itinerary->HotelConfirmation->RateInfos->RateInfo->ChargeableRateInfo->{'@surchargeTotal'})) {
                        $taxes += $itinRes->Itinerary->HotelConfirmation->RateInfos->RateInfo->ChargeableRateInfo->{'@surchargeTotal'};
                    }
                    $totalWithoutTaxes[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->RateInfos->RateInfo->ChargeableRateInfo->{'@nightlyRateTotal'};
                    $cancellation[$i] = $itinRes->Itinerary->HotelConfirmation[$i]->cancellationPolicy;
                }
            } else {
                $rooms[0]['numberOfAdults'] = $itinRes->Itinerary->HotelConfirmation->numberOfAdults;
                $rooms[0]['numberOfChildren'] = $itinRes->Itinerary->HotelConfirmation->numberOfChildren;
                $rooms[0]['firstName'] = $itinRes->Itinerary->HotelConfirmation->ReservationGuest->firstName;
                $rooms[0]['lastName'] = $itinRes->Itinerary->HotelConfirmation->ReservationGuest->lastName;
                $confirmationNumber[0] = $itinRes->Itinerary->HotelConfirmation->confirmationNumber;
                $roomDescription[0] = $itinRes->Itinerary->HotelConfirmation->roomDescription;
                $tripAdvisor[0] = "<b>Trip Advisor Info:This hotel is rated: " . $itinRes->Itinerary->HotelConfirmation->Hotel->tripAdvisorRating . "</b>";
                $checkInDate[0] = $itinRes->Itinerary->HotelConfirmation->arrivalDate;
                $checkOutDate[0] = $itinRes->Itinerary->HotelConfirmation->departureDate;
                $numberOfNights[0] = $itinRes->Itinerary->HotelConfirmation->nights;
                $currencyCode[0] = $itinRes->Itinerary->HotelConfirmation->RateInfos->RateInfo->ChargeableRateInfo->{'@currencyCode'};
                $totalPrice += (float) $itinRes->Itinerary->HotelConfirmation->RateInfos->RateInfo->ChargeableRateInfo->{'@total'};
                if (isset($itinRes->Itinerary->HotelConfirmation->RateInfos->RateInfo->ChargeableRateInfo->{'@surchargeTotal'})) {
                    $taxes += $itinRes->Itinerary->HotelConfirmation->RateInfos->RateInfo->ChargeableRateInfo->{'@surchargeTotal'};
                }
                $totalWithoutTaxes[0] = $itinRes->Itinerary->HotelConfirmation->RateInfos->RateInfo->ChargeableRateInfo->{'@nightlyRateTotal'};
                $cancellation[0] = $itinRes->Itinerary->HotelConfirmation->cancellationPolicy;
            }
            $smartyData = $smarty->createData();


            $smartyData->assign("itinerariId", $itinRes->Itinerary->itineraryId);
            // array
            $smartyData->assign("confirmationNumber", $confirmationNumber);
            // Room type
            $smartyData->assign("roomDescription", $roomDescription);
            // tripAdvisor rating
            $smartyData->assign("tripAdvisorRating", $tripAdvisor);
            $smartyData->assign("checkInDate", $checkInDate);
            $smartyData->assign("checkOutDate", $checkOutDate);
            $smartyData->assign("numberOfNights", $numberOfNights);
            // array nume - copii
            $smartyData->assign("rooms", $rooms);
            $smartyData->assign("roomsNumber", $numberOfRooms);
            $smartyData->assign("currency", $currencyCode);
            $smartyData->assign("totalPrice", $totalPrice);
            $smartyData->assign("taxRecoveryChargesAndFees", $taxes);
            $smartyData->assign("totalNoTax", $totalWithoutTaxes);
            $smartyData->assign("billingAddress", $itinRes->Itinerary->Customer->CustomerAddresses->address1);
            $smartyData->assign("emailAddress", $itinRes->Itinerary->Customer->email);
            $smartyData->assign("phoneNumber", $itinRes->Itinerary->Customer->homePhone);
            $smartyData->assign("cancelationPolicy", $cancellation);
            //itineraryId and email
            if (!empty($_SESSION["logged_user"]) && $_SESSION["logged_bool"] != false) {
                include_once 'ItineraryDBclass.php';
                $varIt = new ItineraryDBclass();
                $varIt->connectToDb();
                $bookingData = $varIt->getBookings($_SESSION["logged_user"]);
            }
            $smartyData->assign("booking", $bookingData);
            $smartyData->assign("bookingCount", count($bookingData));

            echo '<body style="position: relative; top:-16px; " >';
            $smarty->display('header.tpl');
            $smarty->display('manageBookings.tpl', $smartyData);
            $smarty->display('footer.tpl');
            echo "</body";
        } catch (Exception $ex) {
            $smarty->display('preHeader.tpl');
            echo '<body style="position: relative; top:-16px;">';
            $smarty->display('header.tpl');
            $smarty->display('flashyBox.tpl');
            $smarty->display('error.tpl');
            $smarty->display('footer.tpl');
            echo "</body";
        }
    } else {
        try{
            $bookingData = array();
        if (!empty($_SESSION["logged_user"]) && $_SESSION["logged_bool"] != false) {
            include_once 'ItineraryDBclass.php';
            $varIt = new ItineraryDBclass();
            $varIt->connectToDb();
            $bookingData = $varIt->getBookings($_SESSION["logged_user"]);
        }
        //itineraryId and email
        $smartyData = $smarty->createData();
        $smartyData->assign("booking", $bookingData);
        $smartyData->assign("bookingCount", count($bookingData));
        $smarty->display('preHeader.tpl');
        echo '<body style="position: relative; top:-16px;">';
        $smarty->display('header.tpl');
        $smarty->display('flashyBox.tpl');
        $smarty->display('manageBookingsEmpty.tpl', $smartyData);
        //echo '<h3>You need to make a booking first. There weren\'t any bookings in the database.</h3>';
        $smarty->display('footer.tpl');
        echo "</body";
        } catch(Exception $ex){};
    }
    ?>
</html>